from flask import g, request, current_app as app
from app.db.models.audits import Audit


def begin():
    app.logger.info(f'{request.remote_addr} - {request.method} {request.full_path}')


def end(response):
    app.logger.info(f'{request.remote_addr} - {request.method} {request.full_path} {response.status} -')
    return response


"""module
task: '监测任务',
asset: '资产变动监测',
job: '监测任务配置',
vul: '漏洞扫描',
assets: '资产扫描',
ssl: 'SSL配置监测',
aviliable: '可用性监控',
content: '内容安全监测',
alert: '告警/通知',
ping: 'Ping监测',
http: 'HTTP监测',
site-portrait: '网站画像',
billing: '套餐/充值',
user: '用户管理',
news: '新闻/公告',
cloud: '云防联动',
notification: '站内信',
api: API管理,
inspection: 安全巡检
report_push: 报告推送
change_check_page: 篡改监测样本管理
"""

"""actionType
- 创建：create
- 查看：read
- 更新：update
- 删除：delete
"""


class BaseAudit:
    module = ''
    action_type = ''

    def __init__(self, affects='', **kwargs) -> None:
        if isinstance(affects, list):
            affects = affects
        elif isinstance(affects, str):
            affects = [affects]
        else:
            affects = []

        ip = ip if (ip := kwargs.pop('ip', None)) else getattr(g, "ip", "")
        self.audit = Audit(
            type=log_type if (log_type := kwargs.pop('log_type', None)) else g.log_type,
            logSource=logSource if (logSource := kwargs.pop('logSource', None)) else g.logSource,
            username=operator if (operator := kwargs.pop('operator', None)) else g.operator,
            ip=ip if ip != "is_defense" else "",
            module=self.module,
            actionType=self.action_type,
            affects=affects,
            uid=uid if (uid := kwargs.pop('uid', None)) else g.audit_uid.__str__(),
            **kwargs
        )

    def info(self, action_message, detail):
        self.audit.level = 'info'
        self._save(action_message, detail)

    def error(self, action_message, detail):
        self.audit.level = 'error'
        self._save(action_message, detail)

    def _save(self, action_message, detail):
        self.audit.actionMessage = action_message
        self.audit.detail = detail
        self.audit.message = action_message
        self.audit.save()

    def save(self):
        self.audit.save()


class CAssetLog(BaseAudit):
    module = 'asset'
    action_type = 'create'
    name = '资产管理'


class DAssetLog(CAssetLog):
    action_type = 'delete'


class UAssetLog(CAssetLog):
    action_type = 'update'


class CJobLog(BaseAudit):
    module = 'job'
    action_type = 'create'
    name = '监测任务配置'


class RJobLog(CJobLog):
    action_type = 'read'


class UJobLog(CJobLog):
    action_type = 'update'


class DJobLog(CJobLog):
    action_type = 'delete'


class CCloudLog(BaseAudit):
    module = 'cloud'
    action_type = 'create'
    name = '云防联动'


class RCloudLog(CCloudLog):
    action_type = 'read'


class UCloudLog(CCloudLog):
    action_type = 'update'


class DCloudLog(CCloudLog):
    action_type = 'delete'


class CMailLog(BaseAudit):
    module = 'alert'
    action_type = 'create'
    name = '告警/通知'


class UMailLog(CMailLog):
    action_type = 'update'


class CPackages(BaseAudit):
    module = 'billing'
    action_type = 'create'
    name = '套餐/充值'


class UPackages(CPackages):
    action_type = 'update'


class CUserLog(BaseAudit):
    module = 'user'
    action_type = 'create'
    name = '用户管理'


class UUserLog(BaseAudit):
    module = 'user'
    action_type = 'update'
    name = '用户管理'


class LoginLog(BaseAudit):
    module = '@kuiper/auth'
    action_type = 'create'
    name = '登录'


class LogoutLog(BaseAudit):
    module = '@kuiper/auth'
    action_type = 'create'
    name = '退出登录'


class UTaskLog(BaseAudit):
    module = 'task'
    action_type = 'update'
    name = ''


class DTaskLog(UTaskLog):
    action_type = 'delete'
    name = '监测任务'


class CAssetTaskLog(BaseAudit):
    module = 'asset_task'
    action_type = 'create'
    name = '任务管理'


class UAssetTaskLog(CAssetTaskLog):
    action_type = 'update'


class DAssetTaskLog(CAssetTaskLog):
    action_type = 'delete'


class CAlertStrategyLog(BaseAudit):
    module = 'alert_strategy'
    action_type = 'create'
    name = '告警策略'


class UAlertStrategyLog(CAlertStrategyLog):
    action_type = 'update'


class DAlertStrategyLog(CAlertStrategyLog):
    action_type = 'delete'


class CApiLog(BaseAudit):
    module = 'api'
    action_type = 'create'
    name = 'API管理'


class UApiLog(BaseAudit):
    module = 'api'
    action_type = 'update'
    name = 'API管理'


class RSecurityInspectionLog(BaseAudit):
    module = 'inspection'
    action_type = 'read'
    name = '安全巡检'


class USecurityInspectionLog(RSecurityInspectionLog):
    action_type = 'update'


class CSecurityInspectionLog(RSecurityInspectionLog):
    action_type = 'create'


class DSecurityInspectionLog(RSecurityInspectionLog):
    action_type = 'delete'


class CProfileTemplateLog(BaseAudit):
    module = 'profile_template'
    action_type = 'create'
    name = '漏洞策略模板'


class UProfileTemplateLog(CProfileTemplateLog):
    action_type = 'update'


class DProfileTemplateLog(CProfileTemplateLog):
    action_type = 'delete'


class CAlertReceiversLog(BaseAudit):
    module = 'alertReceiver'
    action_type = 'create'
    name = '告警人联系人'


class UAlertReceiversLog(CAlertReceiversLog):
    action_type = 'update'


class DAlertReceiversLog(CAlertReceiversLog):
    action_type = 'delete'


class ReportPushLog(BaseAudit):
    module = 'report_push'
    action_type = 'create'
    name = '报告推送'


class CIgnoreWarning(BaseAudit):
    module = 'ignore_warning'
    action_type = 'create'
    name = '忽略威胁'


class DIgnoreWarning(CIgnoreWarning):
    action_type = 'delete'


class UChangeCheckPageLog(BaseAudit):
    module = 'change_check_page'
    action_type = 'update'
    name = '篡改监测样本管理'
